package com.client.qiuqiu;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;


public class AddFriendFragment extends Fragment{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private Button addButton;
    private EditText friendID;
    private String addFriendIDActionState = "";

    public void setAddFriendIDActionState(String addFriendIDActionState) {
        this.addFriendIDActionState = addFriendIDActionState;
    }

    public AddFriendFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AddFriendFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AddFriendFragment newInstance(String param1, String param2) {
        AddFriendFragment fragment = new AddFriendFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add_friend, container, false);
    }

    //绑定控件
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.friendID = view.findViewById(R.id.addFrinedID);
        this.addButton = view.findViewById(R.id.button_addFriend);
        addButton.setOnClickListener(new addFriendbtnclick());
    }

    //滑回，会Pause，在此复原或刷新内容
    @Override
    public void onPause() {
        super.onPause();
        friendID.setText(null);
    }

    class  addFriendbtnclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            addFriend_Thread addFriend_thread = new addFriend_Thread();
            addFriend_thread.start();
            try {
                addFriend_thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(addFriendIDActionState.equals("false")){
                showToast("Sorry, failed to add a friend.");
                showToast("╮(╯▽╰)╭");
            }else{
                showToast("Adding a friend succeeded!");
                showToast("（づ￣3￣）づ╭❤～");
            }
        }
    }

    private void showToast(String message){
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    class addFriend_Thread extends Thread{
        public void run(){
            String friendIDstr = friendID.getText().toString();

            try {
                User user = ((User)getActivity().getApplicationContext());
                //获取服务器端的输出流
                PrintStream printStream = new PrintStream(user.getSocket().getOutputStream());

                //发送服务端事件
                String msg = "AddFriend";
                printStream.println(msg);
                //发送FriendID
                printStream.println(friendIDstr);

                //接受服务端反馈: 确认登录是否成功
                Scanner scanner = new Scanner(user.getSocket().getInputStream());
                msg = scanner.nextLine();

                if(msg.equals("false")){
                    setAddFriendIDActionState("false");
                    return;
                }else {
                    setAddFriendIDActionState("true");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}